#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <Application.h>
#include <CommonUtils.h>
#include <QDebug>
#include <talkwindow/talkwindow.h>

MainWindow::MainWindow(QWidget* parent)
    : BasicWindows(parent),
      ui(new Ui::MainWindow) {
    ui->setupUi(this);
    init();
    // delete ui->titlebar;
    // ui->titlebar = new TitleBar(this);
    // initTitleBar(ui->titlebar);
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::onAddSubWindow(QWidget* subWindow) {
    connect(subWindow, SIGNAL(signalButtonCloseClicked()), this, SLOT(onButtonCloseClicked()));
    connect(subWindow, SIGNAL(signalButtonMaxClicked()), this, SLOT(onButtonMaxClicked()));
    connect(subWindow, SIGNAL(signalButtonMinClicked()), this, SLOT(onButtonMinClicked()));
    connect(subWindow, SIGNAL(signalButtonRestoreClicked()), this, SLOT(onButtonRestoreClicked()));
    connect(subWindow, SIGNAL(signalButtonRestoreClicked()), this, SLOT(onButtonRestoreClicked()));
    connect(this, SIGNAL(signalWindowRestore()), subWindow, SLOT(onWindowRestore()));
    // connect(this, SIGNAL(signalNewMessage()), subWindow, SLOT(onNewMessage()));
}

void MainWindow::onHeadLabelClicked() {
    infoForm->setUserInfo(Application::Myself());
    infoForm->move(QPoint(pos().x() + 45, pos().y() + 25));
    infoForm->show();
}

void MainWindow::onSetUserInfo() {
    auto userInfo = Application::Myself();
    ui->label_head->setHeadshot(QString::fromStdString(userInfo.headshot()));
}

void MainWindow::onSignalToggleChatListForm() {
    ui->btn_chat->setChecked(true);
    ui->stackedWidgetList->setCurrentWidget(chatListForm);
}

void MainWindow::init() {
    loadStyleSheet("mainwindow");
    initTitleBar();
    initWindowManager();
    initNoneWindow();
    initChatListForm();
    initContactForm();
    initInfoForm();
    connect(Application::instance(), &Application::signalLogin, this, &MainWindow::onLogin);
    connect(Application::instance(), &Application::signalSetUserInfo, this, &MainWindow::onSetUserInfo);

    ui->splitter->setStretchFactor(0, 1);
    ui->splitter->setStretchFactor(1, 9);
    ui->edit_search->setClearButtonEnabled(true);
}

void MainWindow::initTitleBar() {
    ui->titlebar->setButtonType(ButtonType::MIN_MAX_BUTTON);

    connect(ui->titlebar, &TitleBar::signalButtonCloseClicked, this, &MainWindow::onButtonCloseClicked);
    connect(ui->titlebar, &TitleBar::signalButtonMaxClicked, this, &MainWindow::onButtonMaxClicked);
    connect(ui->titlebar, &TitleBar::signalButtonMinClicked, this, &MainWindow::onButtonMinClicked);
    connect(ui->titlebar, &TitleBar::signalButtonRestoreClicked, this, &MainWindow::onButtonRestoreClicked);
}

void MainWindow::initWindowManager() {
    windowManager = new WindowManager();
    windowManager->setStackedWidget(ui->stackedWidget);
    connect(windowManager, &WindowManager::signalAddSubWindow, this, &MainWindow::onAddSubWindow);
}

void MainWindow::initNoneWindow() {
    noneWindow = new NoneWindow(ui->stackedWidget);
    windowManager->addSubWindow("None", noneWindow);
    windowManager->setCurrentWidget(noneWindow);
    connect(noneWindow, &NoneWindow::signalButtonCloseClicked, this, &MainWindow::onButtonCloseClicked);
    connect(noneWindow, &NoneWindow::signalButtonMaxClicked, this, &MainWindow::onButtonMaxClicked);
    connect(noneWindow, &NoneWindow::signalButtonMinClicked, this, &MainWindow::onButtonMinClicked);
    connect(noneWindow, &NoneWindow::signalButtonRestoreClicked, this, &MainWindow::onButtonRestoreClicked);
    connect(noneWindow, &NoneWindow::signalButtonRestoreClicked, this, &MainWindow::onButtonRestoreClicked);
    connect(this, &MainWindow::signalWindowRestore, noneWindow, &NoneWindow::onWindowRestore);
}

void MainWindow::initChatListForm() {
    chatListForm = new ChatListForm(windowManager, ui->stackedWidgetList);
    windowManager->setChatListForm(chatListForm);

    ui->stackedWidgetList->addWidget(chatListForm);
    connect(ui->btn_chat, &QPushButton::clicked, [this]() {
        ui->stackedWidgetList->setCurrentWidget(chatListForm);
        chatListForm->onSetCurrent();
    });
}

void MainWindow::initContactForm() {
    contactForm = new ContactForm(windowManager, ui->stackedWidgetList);
    ui->stackedWidgetList->addWidget(contactForm);

    connect(contactForm, SIGNAL(signalToggle()), SLOT(onSignalToggleChatListForm()));

    connect(ui->btn_user, &QPushButton::clicked, [this]() {
        ui->stackedWidgetList->setCurrentWidget(contactForm);
        ui->stackedWidget->setCurrentWidget(this->noneWindow);
    });
}

void MainWindow::initInfoForm() {
    infoForm = new UserInfoForm();
    infoForm->hide();
    this->installEventFilter(infoForm);
    connect(ui->label_head, &QClickLabel::clicked, this, &MainWindow::onHeadLabelClicked);
}

void MainWindow::setContentMargin(int margin) {
    ui->layout_content->setMargin(margin);
}

void MainWindow::onLogin(int code) {
    if (code) return;

    auto myself = Application::Myself();

    ui->label_head->setHeadshot(QString::fromStdString(myself.headshot()));

    auto app = Application::instance();

    this->show();
}
void MainWindow::on_btn_menu_clicked() {
}

void MainWindow::on_btn_headMenu_clicked() {
    auto headmuneForm = new HeadMenuForm(this);
    headmuneForm->hide();

    auto hwidth  = headmuneForm->width();
    auto hheight = headmuneForm->height();
    headmuneForm->move(QPoint(pos().x() + hwidth + 40, pos().y() + hheight - 15));
    headmuneForm->show();
}
